﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 桌面相关;

public partial class DragDropPanel : Form
{


    private Panel dropPanel;
    private Label infoLabel;

    public DragDropPanel()
    {
        InitializeComponent();

        
        // 窗体设置
        this.Text = "文件拖放面板";
        this.Size = new Size(500, 400);
        this.StartPosition = FormStartPosition.CenterScreen;

        // 创建放置面板
        dropPanel = new Panel();
        dropPanel.Dock = DockStyle.Fill;
        dropPanel.BackColor = Color.LightGray;
        dropPanel.BorderStyle = BorderStyle.FixedSingle;

        // 启用拖放功能
        dropPanel.AllowDrop = true;

        // 信息标签
        infoLabel = new Label();
        infoLabel.Text = "拖放文件到此处";
        infoLabel.AutoSize = false;
        infoLabel.Dock = DockStyle.Top;
        infoLabel.TextAlign = ContentAlignment.MiddleCenter;
        infoLabel.Height = 30;
        infoLabel.BackColor = Color.WhiteSmoke;
        infoLabel.Font = new Font("Microsoft YaHei", 10, FontStyle.Bold);

        // 添加控件
        dropPanel.Controls.Add(infoLabel);
        this.Controls.Add(dropPanel);



        this.FormBorderStyle = FormBorderStyle.Sizable;
        this.AllowTransparency = true;

        // 设置背景透明
        this.BackColor = Color.Black;
        this.TransparencyKey = Color.Black;
        this.Opacity = 1.0;

        // 确保内容不透明
        foreach (Control control in this.Controls)
        {
            control.BackColor = Color.Transparent;
        }

        // 设置拖放事件
        SetUpDragDropEvents();
    }

    private void SetUpDragDropEvents()
    {
        // 拖入时触发
        dropPanel.DragEnter += (sender, e) =>
        {
            // 检查拖入的数据类型
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                e.Effect = DragDropEffects.Copy;
                dropPanel.BackColor = Color.Transparent;
                infoLabel.Text = "释放鼠标以放置文件";
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        };

        // 拖出时恢复颜色
        dropPanel.DragLeave += (sender, e) =>
        {
            dropPanel.BackColor = Color.Transparent;
            infoLabel.Text = "拖放文件到此处";
        };

        // 放置时触发
        dropPanel.DragDrop += (sender, e) =>
        {
            dropPanel.BackColor = Color.Transparent;
            infoLabel.Text = "拖放文件到此处";

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                ProcessDroppedFiles(files);
            }
        };
    }

    private void ProcessDroppedFiles(string[] files)
    {
        // 清空面板中原有内容（保留infoLabel）
        for (int i = dropPanel.Controls.Count - 1; i >= 0; i--)
        {
            if (dropPanel.Controls[i] != infoLabel)
            {
                dropPanel.Controls.RemoveAt(i);
            }
        }

        // 显示拖入的文件
        int yPos = infoLabel.Bottom + 10;
        foreach (string file in files)
        {
            FileInfo fileInfo = new FileInfo(file);

            // 创建文件项控件
            Panel fileItem = new Panel();
            fileItem.Width = dropPanel.Width - 20;
            fileItem.Height = 60;
            fileItem.Location = new Point(10, yPos);
            fileItem.BackColor = Color.White;
            fileItem.BorderStyle = BorderStyle.FixedSingle;

            // 文件图标
            PictureBox iconBox = new PictureBox();
            iconBox.Image = Icon.ExtractAssociatedIcon(file).ToBitmap();
            iconBox.SizeMode = PictureBoxSizeMode.StretchImage;
            iconBox.Size = new Size(40, 40);
            iconBox.Location = new Point(10, 10);

            // 文件信息
            Label nameLabel = new Label();
            nameLabel.Text = fileInfo.Name;
            nameLabel.AutoSize = false;
            nameLabel.Width = fileItem.Width - 70;
            nameLabel.Location = new Point(60, 10);
            nameLabel.Font = new Font("Microsoft YaHei", 9, FontStyle.Bold);

            Label pathLabel = new Label();
            pathLabel.Text = fileInfo.FullName;
            pathLabel.AutoSize = false;
            pathLabel.Width = fileItem.Width - 70;
            pathLabel.Location = new Point(60, 30);
            pathLabel.Font = new Font("Microsoft YaHei", 8);

            // 添加到文件项
            fileItem.Controls.Add(iconBox);
            fileItem.Controls.Add(nameLabel);
            fileItem.Controls.Add(pathLabel);

            // 添加到面板
            dropPanel.Controls.Add(fileItem);

            yPos += fileItem.Height + 10;
        }

        // 显示操作结果
        infoLabel.Text = $"已添加 {files.Length} 个文件";
    }
    private void DragDropPanel_Load(object sender, EventArgs e)
    {

    }
}


